<template>
	<view>
		<cu-custom bgColor="bg-blue"  :isBack="true"><block slot="content">{{title}}</block></cu-custom>
		<view class="cu-bar bg-white solid-bottom margin-top">
			<view class="action">
				<text class="cuIcon-title text-orange "></text> {{title}}列表
			</view>
		</view>
		<view class="cu-list menu-avatar">
			<view class="cu-item" @tap="showAdvertising(item)" v-for="(item,index) in advertisingList" :key="index">
				
				<image v-if="item.cover.lastIndexOf('.PDF')>0||item.cover.lastIndexOf('.pdf')>0" class="cu-avatar lg" src="/static/img/pdf.png" mode="scaleToFill"></image>
				<image v-else class="cu-avatar lg" :src="item.cover" mode="scaleToFill"></image>
				<view class="content">
					<view class="text-grey">{{item.title}}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	
	import { listSimpleAdvertising } from '../../../api/modules/simpleAdvertising.js'
	export default {
		data() {
			return {
				title:"广告",
				type:"0",
				advertisingList:[]
			}
		},
		onLoad(options) {
			this.type=options.type
			if(this.type==0){
				this.title="广告"
			}else if(this.type==1){
				this.title="农业部认证"
			}else if(this.type==2){	
				this.title="服务说明"
			}else if(this.type==3){
				this.title="猎弧一号—弧菌克星"
			}
		},
		onReady() {
			this.getSimpleAdvertisingList()
		},
		methods: {
			getSimpleAdvertisingList(){
				let self=this
				self.advertisingList=[]
				let data={pageNum:1,pageSize:10,type:self.type,delFlag:0}
				listSimpleAdvertising(data).then(res=>{
					if(res.data.code==200){
						self.advertisingList=res.data.rows
					}
				})
			},
			showAdvertising(item){
				if(item.cover&&(item.cover.lastIndexOf(".PDF")>0||item.cover.lastIndexOf(".pdf")>0)){
					//打开pdf文件
					uni.showLoading({
					  title: '加载中...',
					})
						console.log('加载中');
					uni.downloadFile({
					  url: item.cover,
					  success: function (res) {
						console.log('downloadFile', res);
						const filePath = res.tempFilePath
						uni.openDocument({
						  filePath: filePath,
						  success: function (res) {
							console.log(res);
							console.log('打开文档成功')
							uni.hideLoading()
						  },
						  fail: function (res) { 
							uni.hideLoading()
							uni.showToast({
								title:"打开文件失败"
							})
							console.log('打开失败')
						  },
						})
					  }
					})
				}else{
					let imgs=[]
					imgs.push(item.cover)
					uni.previewImage({
						urls:imgs,
						current: item.cover
					});
				}
			}
			
		}
	}
</script>

<style>

</style>
